﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;

public partial class Main_Company_DefaultLedger : System.Web.UI.Page
{
    Datautility DU = new Datautility();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //BindCustomData();
           
            ChkCST.Checked = true;
            ChkPurchase.Checked = true;
            ChkSales.Checked = true;
            ChkTax.Checked = true;
            ChkVAT.Checked = true;
            lblTotal.Text = "";
            BindGridLedger1();
           // BindGridLedger2();
           // lblMsg.Text = "";
        }
                                                                                                
    }

    protected void btnCreate_Click(Object sender, EventArgs e)
    {
        Insertdata();
        if (this.Tabs.ActiveTabIndex == 1)
        {
            ChkSelectAll.Checked = false;
            BindGridLedger2();
            hidActiveIndex.Value = "1";
        }
        else
        {
            ChkSelectAll.Checked = false;
            BindGridLedger1();
            hidActiveIndex.Value = "0";
        }
        lblMsg.Text = "Selected ledger(s) created successfully !";
    }

    protected void Insertdata()
    {
        //GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
        //int d = row.RowIndex;
        string chkcostcentre = "";
        if (this.Tabs.ActiveTabIndex == 0)
        {
            int Count = Convert.ToInt32(grdDefaultLedger2.Rows.Count);
            for (int i = 0; i < Count; i++)
            {
                CheckBox chkSelect = (CheckBox)grdDefaultLedger2.Rows[i].FindControl("ChkSelect");
                if (chkSelect.Checked == true)
                {
                    HiddenField hidParentGroup = (HiddenField)grdDefaultLedger2.Rows[i].FindControl("hidParentTaxTypeId2");
                    HiddenField hidgid = (HiddenField)grdDefaultLedger2.Rows[i].FindControl("hidGid2");
                    Label LedgerName = (Label)grdDefaultLedger2.Rows[i].FindControl("lblLedgerName2");
                    if (hidgid.Value.ToString() == "19")
                    {
                        chkcostcentre = "0";
                    }
                    else
                    {
                        chkcostcentre = "1";
                    }

                    SqlParameter[] sqlpara = {
                                     new SqlParameter("@PGID",hidParentGroup.Value.ToString()),
                                     new SqlParameter("@LNAME",LedgerName.Text),
                                     new SqlParameter("@LANAME",""),
                                     new SqlParameter("@ACTOPENING","0"),
                                     new SqlParameter("@BILLWISE","0"),
                                     new SqlParameter("@ALWSUB","0"),
                                     new SqlParameter("@TAXAPPLY","1"),
                                     new SqlParameter("@COSTCENTREAPPLY",chkcostcentre),
                                     new SqlParameter("@TDSAPPLY","0"),
                                     new SqlParameter("@STAPPLY","0"),
                                     new SqlParameter("@CName",Session["username"].ToString())
                                 };
                    DU.executeprocedure("usp_insert_ACC_LEDMAST", sqlpara);

                    string strsql = "select ident_current('ACC_LEDMAST')as LID";
                    DataTable dtLID = DU.getdata(strsql);
                    int lid = Convert.ToInt32(dtLID.Rows[0]["LID"].ToString());

                    SqlParameter[] sqlparame = {
                                       new SqlParameter("@LID",lid), 
                                       new SqlParameter("@ACTIVE","1"), 
                                       new SqlParameter("@TCS","0"), 
                                       new SqlParameter("@CREDITPERIOD","0"), 
                                       new SqlParameter("@CREDITLIMIT","0"), 
                                       new SqlParameter("@SPRID",""), 
                                       new SqlParameter("@PURTAXMAINCLASSID","0"), 
                                       new SqlParameter("@SALETAXMAINCLASSID","0"), 
                                       new SqlParameter("@SUBTAXID",""),
                                       new SqlParameter("@CNAME",Session["username"].ToString())
                                   };
                    DU.executeprocedure("USP_ACC_SaveLedgerOtherDetails", sqlparame);
                }
            }
        }
        else
        {
            int Count = Convert.ToInt32(grdDefaultLedger1.Rows.Count);
            for (int i = 0; i < Count; i++)
            {
                CheckBox chkSelect = (CheckBox)grdDefaultLedger1.Rows[i].FindControl("ChkSelect");
                if (chkSelect.Checked == true)
                {
                    HiddenField hidsubclass = (HiddenField)grdDefaultLedger1.Rows[i].FindControl("hidMainClassID");
                    HiddenField hidmainclass = (HiddenField)grdDefaultLedger1.Rows[i].FindControl("hidSubClassID");
                    HiddenField hidgid = (HiddenField)grdDefaultLedger1.Rows[i].FindControl("hidGid");
                    HiddenField hidTaxType = (HiddenField)grdDefaultLedger1.Rows[i].FindControl("hidTaxTypeID");
                    HiddenField hidParentGroup = (HiddenField)grdDefaultLedger1.Rows[i].FindControl("hidParentTaxTypeId");
                    Label LedgerName = (Label)grdDefaultLedger1.Rows[i].FindControl("lblLedgerName");
                    if (hidgid.Value.ToString() == "19")
                    {
                        chkcostcentre = "0";
                    }
                    else
                    {
                        chkcostcentre = "1";
                    }

                    SqlParameter[] sqlpara = {
                                     new SqlParameter("@PGID",hidgid.Value.ToString()),
                                     new SqlParameter("@LNAME",LedgerName.Text),
                                     new SqlParameter("@LANAME",""),
                                     new SqlParameter("@ACTOPENING","0"),
                                     new SqlParameter("@BILLWISE","0"),
                                     new SqlParameter("@ALWSUB","0"),
                                     new SqlParameter("@TAXAPPLY","1"),
                                     new SqlParameter("@COSTCENTREAPPLY",chkcostcentre),
                                     new SqlParameter("@TDSAPPLY","0"),
                                     new SqlParameter("@STAPPLY","0"),
                                     new SqlParameter("@CName",Session["username"].ToString())
                                 };
                    DU.executeprocedure("usp_insert_ACC_LEDMAST", sqlpara);

                    string strsql = "select ident_current('ACC_LEDMAST')as LID";
                    DataTable dtLID = DU.getdata(strsql);
                    int lid = Convert.ToInt32(dtLID.Rows[0]["LID"].ToString());

                    SqlParameter[] sqlparam = {
                                      new SqlParameter("@LID",lid), 
                                      new SqlParameter("@TAXTYPEID",hidTaxType.Value.ToString()), 
                                      new SqlParameter("@MAINCLASSID",hidmainclass.Value.ToString()), 
                                      new SqlParameter("@SUBCLASSID",hidsubclass.Value.ToString()), 
                                      new SqlParameter("@TAXDETAILID","-1"), 
                                      new SqlParameter("@CNAME",Session["username"].ToString())
                                  };
                    DU.executeprocedure("USP_ACC_SaveLedgerTaxDetails", sqlparam);

                    SqlParameter[] sqlparame = {
                                       new SqlParameter("@LID",lid), 
                                       new SqlParameter("@ACTIVE","1"), 
                                       new SqlParameter("@TCS","0"), 
                                       new SqlParameter("@CREDITPERIOD","0"), 
                                       new SqlParameter("@CREDITLIMIT","0"), 
                                       new SqlParameter("@SPRID",""), 
                                       new SqlParameter("@PURTAXMAINCLASSID","0"), 
                                       new SqlParameter("@SALETAXMAINCLASSID","0"), 
                                       new SqlParameter("@SUBTAXID",""),
                                       new SqlParameter("@CNAME",Session["username"].ToString())
                                   };
                    DU.executeprocedure("USP_ACC_SaveLedgerOtherDetails", sqlparame);

                    SqlParameter[] sqlparamet = {
                                        new SqlParameter("@SELECTIONTYPE","1"), 
                                        new SqlParameter("@LID",lid),
                                        new SqlParameter("@GID",hidgid.Value.ToString()), 
                                        new SqlParameter("@MAINCLASSID",hidmainclass.Value.ToString()), 
                                        new SqlParameter("@SUBCLASSID",hidsubclass.Value.ToString()), 
                                        new SqlParameter("@TAXTYPEID",hidTaxType.Value.ToString())                                    
                                    };
                    DU.executeprocedure("USP_ACC_UpdateTaxDetailsForDefaultLedgers", sqlparamet);
                }
            }
        }
    }

    protected void BindGridLedger1()
    {
        SqlParameter[] sqlparam = {
                                      new SqlParameter("@ISVATCHECKED",Convert.ToByte(ChkVAT.Checked)),      
                                      new SqlParameter("@ISCSTCHECKED",Convert.ToByte(ChkCST.Checked)),      
                                      new SqlParameter("@ISSALESCHECKED",Convert.ToByte(ChkSales.Checked)),
                                      new SqlParameter("@ISPURCHASECHECKED",Convert.ToByte(ChkPurchase.Checked)),
                                      new SqlParameter("@ISTAXCHECKED",Convert.ToByte(ChkTax.Checked))
                                  };
        DataTable dd = DU.getdatausingprocedure("USP_ACC_GetDefaultLedgersForInventory",sqlparam);
        grdDefaultLedger1.DataSource = dd;
        grdDefaultLedger1.DataBind();
        lblTotal.Text = Convert.ToString(dd.Rows.Count);
    }
    protected void BindGridLedger2()
    {
        SqlParameter[] sqlpara = {
                                      new SqlParameter("@SELECTIONTYPE","3"), 
                                 };
        DataTable dtledg2 = DU.getdatausingprocedure("USP_ACC_GetDefaultLedgers", sqlpara);
        grdDefaultLedger2.DataSource = dtledg2;
        grdDefaultLedger2.DataBind();
        lblTotal.Text = Convert.ToString(dtledg2.Rows.Count);
    }
    protected void ChkSales_CheckedChanged(object sender, EventArgs e)
    {
        BindGridLedger1();
    }
    protected void ChkPurchase_CheckedChanged(object sender, EventArgs e)
    {
        BindGridLedger1();
    }
    protected void ChkCST_CheckedChanged(object sender, EventArgs e)
    {
        BindGridLedger1();
    }
    protected void ChkVAT_CheckedChanged(object sender, EventArgs e)
    {
        BindGridLedger1();
    }
    protected void ChkTax_CheckedChanged(object sender, EventArgs e)
    {
        BindGridLedger1();
    }
    protected void Tabs_ActiveTabChanged(object sender, EventArgs e)
    {
        lblMsg.Text = "";
        if (this.Tabs.ActiveTabIndex == 1)
        {
            
           ChkSelectAll.Checked = false;
           BindGridLedger2();
           hidActiveIndex.Value = "1";
        }
        else 
        {
           ChkSelectAll.Checked = false;
           BindGridLedger1();
           hidActiveIndex.Value = "0";
        }
    }

    //protected void ChkSelect_CheckedChanged(object sender, EventArgs e)
    //{
    //    int count = grdDefaultLedger1.Rows.Count;
    //    int countChecked = 0;
    //    for (int i = 0; i < count; i++)
    //    {
    //        CheckBox chkselect = (CheckBox)grdDefaultLedger1.Rows[i].FindControl("ChkSelect");
    //        if (chkselect.Checked == true)
    //        {
    //            countChecked++;
    //        }
    //    }
        
    //    if (countChecked == count)
    //    {
    //        ChkSelectAll.Checked = true;
    //    }
    //    else
    //    {
    //        ChkSelectAll.Checked = false;
    //    }
    //}
    //protected void ChkSelect2_CheckedChanged(object sender, EventArgs e)
    //{
    //    int count = grdDefaultLedger2.Rows.Count;
    //    int countChecked = 0;
    //    for (int i = 0; i < count; i++)
    //    {
    //        CheckBox chkselect2 = (CheckBox)grdDefaultLedger2.Rows[i].FindControl("ChkSelect2");
    //        if (chkselect2.Checked == true)
    //        {
    //            countChecked++;
    //        }
    //    }
       
    //    if (countChecked == count)
    //    {
    //        ChkSelectAll.Checked = true;
    //    }
    //    else
    //    {
    //        ChkSelectAll.Checked = false;
    //    }
    //}
}
